* Jon Temple and Nicolas Van de Sijpe, 29-May-2017.
* Bootstrap CCE IV SEs.


*************************************************************
* SET UP DO-FILE
*************************************************************
version 14
clear all
set more off, perm
set matsize 2000

local aidinstrument "C:\Users\ec1nv\Dropbox\My Documents\My Papers\Aid and absorption"
local aidinstrumentdata "`aidinstrument'\Data"
local ouranalysis "`aidinstrumentdata'\Results\Our analysis"
local regrtables "`aidinstrument'\Paper\Tables"
// change directory to bootstrap folder:
cd "`ouranalysis'\Bootstrap"


*************************************************************
* BRING IN DATA AND SET UP DATA
*************************************************************
use "`ouranalysis'\JIE data.dta"
d, fulln
su ccode

sort ccode periodid
xtset ccode periodid


*************************************************************
* RENAME VARIABLES
*************************************************************
/* Give slightly shorter names to dependent variables. */
rename househconsGDP hconsGDP
rename govtconsGDP gconsGDP
rename finalconsGDPsum tconsGDPsum 
rename grosscapformGDP grcapformGDP


*************************************************************
* SELECT AID VARIABLE
*************************************************************
gen double aidvar = netaidGDP
d netaidGDP aidvar, fulln
label variable aidvar "Aid"


*************************************************************
* CHOOSE DEPENDENT VARIABLE AND AID INSTRUMENT
*************************************************************
gen double depvar = grcapformGDP
label variable depvar "Dependent variable"
gen double lagdepvar = L.depvar
label variable lagdepvar "Lagged dep. variable"
gen double aidiv = netaidinstr6070GDP


*************************************************************
* DROP SMALL COUNTRIES
*************************************************************
/* First generate sample indicator by country. 
Use dynamic CCE IV for this: */
capture drop incflag
gen incflag=(depvar<. & lagdepvar<. & aidvar<. & aidiv<. /// 
& period != "1960-61" & period != "1962-64" & period != "1965-67" & period != "1968-70")
capture drop incflagsum
bysort ccode (period): egen incflagsum = sum(incflag)
*browse ccode periodid incflag incflagsum
capture drop sampleobs
gen sampleobs=(incflag == 1 & incflagsum >= 6) // 1 for an observation that is included in the sample.
label variable sampleobs "1 if observation is included in full sample"
capture drop samplecountry
gen samplecountry=(incflagsum >= 6) // 1 for a country that is included in the sample.
label variable samplecountry "1 if country is included in full sample"
// Check these:
tab sampleobs, missing
tab samplecountry if periodid == 1, missing


/* Now identify small countries */
d population, fulln
sort ccode periodid
*browse ccode periodid population
/* Designate countries as small if fewer than 500.000 people or population missing.
Use first period in estimation sample for this (1971-73). */
gen small1=((population < 500000 | population == .) & period == "1971-73")
*browse ccode periodid population small1
tab small1 if period == "1971-73", missing
bysort ccode (periodid): egen small = max(small1)
*browse ccode periodid population small1 small
label variable small "1 if small country"

/* List countries; whether or not they would be in full sample; and whether or not they are small. */
list country samplecountry small population if periodid == 1
// can see here: bunch of countries that would otherwise end up in sample.
// These are the countries that would be in the sample but get dropped because they are too small:
list country samplecountry small population if periodid == 1 & samplecountry == 1 & small == 1

/* Now drop small countries from the dataset. */
drop if small == 1

tab incflag if incflagsum >= 6, missing // 1099 observations left.


******************************************************************************
* KEEP ONLY RELEVANT VARIABLES
******************************************************************************
keep country ccode period periodid netaidGDP netaidinstr6070GDP hconsGDP gconsGDP /// 
tconsGDPsum grcapformGDP exportGDP importGDP netimportGDP incflag incflagsum ///
aidvar aidiv depvar lagdepvar

// generate time dummies.
su periodid
local periodidmax = r(max)
foreach num of numlist 1(1)`periodidmax' {
	gen ydummy`num'=(periodid==`num')
}
*browse country ccode period periodid ydummy*


******************************************************************************
* KEEP ONLY COUNTRIES WITH ENOUGH OBSERVATIONS
******************************************************************************
keep if incflagsum >= 6


******************************************************************************
* SET BOOTSTRAP PARAMETERS
******************************************************************************
local reps 1999


capture log close
log using "`ouranalysis'/Bootstrap/Static CCE IV for C; `reps' replications", text replace
******************************************************************************
* BOOTSTRAP FOR STATIC MODEL - HOUSEHOLD CONSUMPTION
******************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set up data and start timer */
timer clear
timer on 1
sort ccode periodid
xtset ccode periodid
replace depvar = hconsGDP
replace lagdepvar = L.depvar

/* Run static CCE IV regression once: */
// construct sample means for all variables:
capture drop mdepvar maid minst
bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1
bysort periodid: egen double maid=mean(aidvar) if incflag == 1
bysort periodid: egen double minst=mean(aidiv) if incflag == 1
// run static CCE IV regression
xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag == 1, /// 
fe cluster(ccode) small
/* Same with partialling out. */
capture drop aidres ivres
capture drop depvarres
qui: xi: xtreg depvar i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1, fe
predict double depvarres, e
qui: xi: xtreg aidvar i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1, fe
predict double aidres, e
qui: xi: xtreg aidiv i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1, fe
predict double ivres, e
// run regression:
xtivreg2 depvarres (aidres = ivres) if incflag==1, fe
scalar beta_CCEIV = _b[aidres]
// partialling out is slightly faster.
// Check FE IV:
xtivreg2 depvar (aidvar = aidiv) ydummy* if incflag == 1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small 
scalar beta_FEIV = _b[aidvar]


/* Write the bootstrap program */
capture program drop bootstaticCCEIV
program define bootstaticCCEIV, rclass
	syntax [if] [in]
	marksample touse
	xtset newcluster periodid
	// FE IV regresssion:
	xtivreg2 depvar (aidvar = aidiv) ydummy* if `touse', fe 
	return scalar FEIV = _b[aidvar]
	// construct sample means for all variables:
	capture drop mdepvar maid minst
	bysort periodid: egen double mdepvar=mean(depvar) if `touse'
	bysort periodid: egen double maid=mean(aidvar) if `touse'
	bysort periodid: egen double minst=mean(aidiv) if `touse'
	capture drop aidres ivres
	capture drop depvarres
	qui: xi: xtreg depvar i.newcluster|mdepvar i.newcluster|maid i.newcluster|minst if `touse', fe
	predict double depvarres, e
	qui: xi: xtreg aidvar i.newcluster|mdepvar i.newcluster|maid i.newcluster|minst if `touse', fe
	predict double aidres, e
	qui: xi: xtreg aidiv i.newcluster|mdepvar i.newcluster|maid i.newcluster|minst if `touse', fe
	predict double ivres, e
	// run regression:
	xtivreg2 depvarres (aidres = ivres) if `touse', fe
	return scalar CCEIV = _b[aidres]
end

/* Conduct the bootstrap: */
xtset, clear
bootstrap FEIV=r(FEIV) CCEIV=r(CCEIV), reps(`reps') seed(8845) cluster(ccode) idcluster(newcluster) /// 
dots level(90) bca saving("Static CCE IV for C", double replace): bootstaticCCEIV if incflag==1 
// seed chosen as random number 0-10000 from http://www.random.org/

/* Summarise results: */
estat bootstrap, all

/* Test equality of coefficients. */
preserve
use "Static CCE IV for C.dta", clear
di beta_FEIV
di beta_CCEIV
correlate FEIV CCEIV, covariance
scalar var_FEIV = r(Var_1)
scalar var_CCEIV = r(Var_2)
scalar cov = r(cov_12)
scalar chi2 = ((beta_FEIV - beta_CCEIV)^2)/(var_FEIV + var_CCEIV - 2*cov)
di chi2
di "p-value is " 1 - chi2(1,chi2)
di "p-value is " chi2tail(1,chi2)
restore

/* Check timer and close log file. */
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
log close


capture log close
log using "`ouranalysis'/Bootstrap/Static CCE IV for G; `reps' replications", text replace
******************************************************************************
* BOOTSTRAP FOR STATIC MODEL - GOVERNMENT CONSUMPTION
******************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set up data and start timer */
timer clear
timer on 1
sort ccode periodid
xtset ccode periodid
replace depvar = gconsGDP
replace lagdepvar = L.depvar

/* Run static CCE IV regression once: */
// construct sample means for all variables:
capture drop mdepvar maid minst
bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1
bysort periodid: egen double maid=mean(aidvar) if incflag == 1
bysort periodid: egen double minst=mean(aidiv) if incflag == 1
// run static CCE IV regression
xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag == 1, /// 
fe cluster(ccode) small // endog(aidvar) first savefirst
/* Same with partialling out. */
capture drop aidres ivres
capture drop depvarres
qui: xi: xtreg depvar i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1, fe
predict double depvarres, e
qui: xi: xtreg aidvar i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1, fe
predict double aidres, e
qui: xi: xtreg aidiv i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1, fe
predict double ivres, e
// run regression:
xtivreg2 depvarres (aidres = ivres) if incflag==1, fe
scalar beta_CCEIV = _b[aidres]
// Check FE IV:
xtivreg2 depvar (aidvar = aidiv) ydummy* if incflag == 1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small 
scalar beta_FEIV = _b[aidvar]

/* Conduct the bootstrap: */
xtset, clear
bootstrap FEIV=r(FEIV) CCEIV=r(CCEIV), reps(`reps') seed(5793) cluster(ccode) idcluster(newcluster) /// 
dots level(90) bca saving("Static CCE IV for G", double replace): bootstaticCCEIV if incflag==1 
// seed chosen as random number 0-10000 from http://www.random.org/

/* Summarise results: */
estat bootstrap, all

/* Calculate test statistic */
preserve
use "Static CCE IV for G.dta", clear
di beta_FEIV
di beta_CCEIV
correlate FEIV CCEIV, covariance
scalar var_FEIV = r(Var_1)
scalar var_CCEIV = r(Var_2)
scalar cov = r(cov_12)
scalar chi2 = ((beta_FEIV - beta_CCEIV)^2)/(var_FEIV + var_CCEIV - 2*cov)
di chi2
di "p-value is " 1 - chi2(1,chi2)
di "p-value is " chi2tail(1,chi2)
restore

/* Check timer and close log file. */
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
log close


capture log close
log using "`ouranalysis'/Bootstrap/Static CCE IV for C plus G; `reps' replications", text replace
******************************************************************************
* BOOTSTRAP FOR STATIC MODEL - TOTAL CONSUMPTION
******************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set up data and start timer */
timer clear
timer on 1
sort ccode periodid
xtset ccode periodid
replace depvar = tconsGDPsum
replace lagdepvar = L.depvar

/* Run static CCE IV regression once: */
// construct sample means for all variables:
capture drop mdepvar maid minst
bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1
bysort periodid: egen double maid=mean(aidvar) if incflag == 1
bysort periodid: egen double minst=mean(aidiv) if incflag == 1
// run static CCE IV regression
xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag == 1, /// 
fe cluster(ccode) small // endog(aidvar) first savefirst
/* Same with partialling out. */
capture drop aidres ivres
capture drop depvarres
qui: xi: xtreg depvar i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1, fe
predict double depvarres, e
qui: xi: xtreg aidvar i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1, fe
predict double aidres, e
qui: xi: xtreg aidiv i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1, fe
predict double ivres, e
// run regression:
xtivreg2 depvarres (aidres = ivres) if incflag==1, fe
scalar beta_CCEIV = _b[aidres]
// Check FE IV:
xtivreg2 depvar (aidvar = aidiv) ydummy* if incflag == 1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small 
scalar beta_FEIV = _b[aidvar]

/* Conduct the bootstrap: */
xtset, clear
bootstrap FEIV=r(FEIV) CCEIV=r(CCEIV), reps(`reps') seed(3340) cluster(ccode) idcluster(newcluster) /// 
dots level(90) bca saving("Static CCE IV for C plus G", double replace): bootstaticCCEIV if incflag==1 
// seed chosen as random number 0-10000 from http://www.random.org/

/* Summarise results: */
estat bootstrap, all

/* Calculate test statistic */
preserve
use "Static CCE IV for C plus G.dta", clear
di beta_FEIV
di beta_CCEIV
correlate FEIV CCEIV, covariance
scalar var_FEIV = r(Var_1)
scalar var_CCEIV = r(Var_2)
scalar cov = r(cov_12)
scalar chi2 = ((beta_FEIV - beta_CCEIV)^2)/(var_FEIV + var_CCEIV - 2*cov)
di chi2
di "p-value is " 1 - chi2(1,chi2)
di "p-value is " chi2tail(1,chi2)
restore

/* Check timer and close log file. */
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
log close


capture log close
log using "`ouranalysis'/Bootstrap/Static CCE IV for I; `reps' replications", text replace
******************************************************************************
* BOOTSTRAP FOR STATIC MODEL - INVESTMENT
******************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set up data and start timer */
timer clear
timer on 1
sort ccode periodid
xtset ccode periodid
replace depvar = grcapformGDP
replace lagdepvar = L.depvar

/* Run static CCE IV regression once: */
// construct sample means for all variables:
capture drop mdepvar maid minst
bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1
bysort periodid: egen double maid=mean(aidvar) if incflag == 1
bysort periodid: egen double minst=mean(aidiv) if incflag == 1
// run static CCE IV regression
xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag == 1, /// 
fe cluster(ccode) small // endog(aidvar) first savefirst
/* Same with partialling out. */
capture drop aidres ivres
capture drop depvarres
qui: xi: xtreg depvar i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1, fe
predict double depvarres, e
qui: xi: xtreg aidvar i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1, fe
predict double aidres, e
qui: xi: xtreg aidiv i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1, fe
predict double ivres, e
// run regression:
xtivreg2 depvarres (aidres = ivres) if incflag==1, fe
scalar beta_CCEIV = _b[aidres]
// Check FE IV:
xtivreg2 depvar (aidvar = aidiv) ydummy* if incflag == 1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small 
scalar beta_FEIV = _b[aidvar]

/* Conduct the bootstrap: */
xtset, clear
bootstrap FEIV=r(FEIV) CCEIV=r(CCEIV), reps(`reps') seed(6156) cluster(ccode) idcluster(newcluster) /// 
dots level(90) bca saving("Static CCE IV for I", double replace): bootstaticCCEIV if incflag==1 
// seed chosen as random number 0-10000 from http://www.random.org/

/* Summarise results: */
estat bootstrap, all

/* Calculate test statistic */
preserve
use "Static CCE IV for I.dta", clear
di beta_FEIV
di beta_CCEIV
correlate FEIV CCEIV, covariance
scalar var_FEIV = r(Var_1)
scalar var_CCEIV = r(Var_2)
scalar cov = r(cov_12)
scalar chi2 = ((beta_FEIV - beta_CCEIV)^2)/(var_FEIV + var_CCEIV - 2*cov)
di chi2
di "p-value is " 1 - chi2(1,chi2)
di "p-value is " chi2tail(1,chi2)
restore

/* Check timer and close log file. */
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
log close


capture log close
log using "`ouranalysis'/Bootstrap/Static CCE IV for X; `reps' replications", text replace
******************************************************************************
* BOOTSTRAP FOR STATIC MODEL - EXPORTS
******************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set up data and start timer */
timer clear
timer on 1
sort ccode periodid
xtset ccode periodid
replace depvar = exportGDP
replace lagdepvar = L.depvar

/* Run static CCE IV regression once: */
// construct sample means for all variables:
capture drop mdepvar maid minst
bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1
bysort periodid: egen double maid=mean(aidvar) if incflag == 1
bysort periodid: egen double minst=mean(aidiv) if incflag == 1
// run static CCE IV regression
xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag == 1, /// 
fe cluster(ccode) small // endog(aidvar) first savefirst
/* Same with partialling out. */
capture drop aidres ivres
capture drop depvarres
qui: xi: xtreg depvar i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1, fe
predict double depvarres, e
qui: xi: xtreg aidvar i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1, fe
predict double aidres, e
qui: xi: xtreg aidiv i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1, fe
predict double ivres, e
// run regression:
xtivreg2 depvarres (aidres = ivres) if incflag==1, fe
scalar beta_CCEIV = _b[aidres]
// Check FE IV:
xtivreg2 depvar (aidvar = aidiv) ydummy* if incflag == 1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small 
scalar beta_FEIV = _b[aidvar]

/* Conduct the bootstrap: */
xtset, clear
bootstrap FEIV=r(FEIV) CCEIV=r(CCEIV), reps(`reps') seed(7569) cluster(ccode) idcluster(newcluster) /// 
dots level(90) bca saving("Static CCE IV for X", double replace): bootstaticCCEIV if incflag==1 
// seed chosen as random number 0-10000 from http://www.random.org/

/* Summarise results: */
estat bootstrap, all

/* Calculate test statistic */
preserve
use "Static CCE IV for X.dta", clear
di beta_FEIV
di beta_CCEIV
correlate FEIV CCEIV, covariance
scalar var_FEIV = r(Var_1)
scalar var_CCEIV = r(Var_2)
scalar cov = r(cov_12)
scalar chi2 = ((beta_FEIV - beta_CCEIV)^2)/(var_FEIV + var_CCEIV - 2*cov)
di chi2
di "p-value is " 1 - chi2(1,chi2)
di "p-value is " chi2tail(1,chi2)
restore

/* Check timer and close log file. */
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
log close


capture log close
log using "`ouranalysis'/Bootstrap/Static CCE IV for M; `reps' replications", text replace
******************************************************************************
* BOOTSTRAP FOR STATIC MODEL - IMPORTS
******************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set up data and start timer */
timer clear
timer on 1
sort ccode periodid
xtset ccode periodid
replace depvar = importGDP
replace lagdepvar = L.depvar

/* Run static CCE IV regression once: */
// construct sample means for all variables:
capture drop mdepvar maid minst
bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1
bysort periodid: egen double maid=mean(aidvar) if incflag == 1
bysort periodid: egen double minst=mean(aidiv) if incflag == 1
// run static CCE IV regression
xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag == 1, /// 
fe cluster(ccode) small // endog(aidvar) first savefirst
/* Same with partialling out. */
capture drop aidres ivres
capture drop depvarres
qui: xi: xtreg depvar i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1, fe
predict double depvarres, e
qui: xi: xtreg aidvar i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1, fe
predict double aidres, e
qui: xi: xtreg aidiv i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1, fe
predict double ivres, e
// run regression:
xtivreg2 depvarres (aidres = ivres) if incflag==1, fe
scalar beta_CCEIV = _b[aidres]
// Check FE IV:
xtivreg2 depvar (aidvar = aidiv) ydummy* if incflag == 1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small 
scalar beta_FEIV = _b[aidvar]

/* Conduct the bootstrap: */
xtset, clear
bootstrap FEIV=r(FEIV) CCEIV=r(CCEIV), reps(`reps') seed(1154) cluster(ccode) idcluster(newcluster) /// 
dots level(90) bca saving("Static CCE IV for M", double replace): bootstaticCCEIV if incflag==1 
// seed chosen as random number 0-10000 from http://www.random.org/

/* Summarise results: */
estat bootstrap, all

/* Calculate test statistic */
preserve
use "Static CCE IV for M.dta", clear
di beta_FEIV
di beta_CCEIV
correlate FEIV CCEIV, covariance
scalar var_FEIV = r(Var_1)
scalar var_CCEIV = r(Var_2)
scalar cov = r(cov_12)
scalar chi2 = ((beta_FEIV - beta_CCEIV)^2)/(var_FEIV + var_CCEIV - 2*cov)
di chi2
di "p-value is " 1 - chi2(1,chi2)
di "p-value is " chi2tail(1,chi2)
restore

/* Check timer and close log file. */
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
log close


capture log close
log using "`ouranalysis'/Bootstrap/Static CCE IV for M minus X; `reps' replications", text replace
******************************************************************************
* BOOTSTRAP FOR STATIC MODEL - NET IMPORTS
******************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set up data and start timer */
timer clear
timer on 1
sort ccode periodid
xtset ccode periodid
replace depvar = netimportGDP
replace lagdepvar = L.depvar

/* Run static CCE IV regression once: */
// construct sample means for all variables:
capture drop mdepvar maid minst
bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1
bysort periodid: egen double maid=mean(aidvar) if incflag == 1
bysort periodid: egen double minst=mean(aidiv) if incflag == 1
// run static CCE IV regression
xi: xtivreg2 depvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag == 1, /// 
fe cluster(ccode) small // endog(aidvar) first savefirst
/* Same with partialling out. */
capture drop aidres ivres
capture drop depvarres
qui: xi: xtreg depvar i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1, fe
predict double depvarres, e
qui: xi: xtreg aidvar i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1, fe
predict double aidres, e
qui: xi: xtreg aidiv i.ccode|mdepvar i.ccode|maid i.ccode|minst if incflag==1, fe
predict double ivres, e
// run regression:
xtivreg2 depvarres (aidres = ivres) if incflag==1, fe
scalar beta_CCEIV = _b[aidres]
// Check FE IV:
xtivreg2 depvar (aidvar = aidiv) ydummy* if incflag == 1 & incflagsum >= 6, fe cluster(ccode) endog(aidvar) first savefirst small 
scalar beta_FEIV = _b[aidvar]

/* Conduct the bootstrap: */
xtset, clear
bootstrap FEIV=r(FEIV) CCEIV=r(CCEIV), reps(`reps') seed(2498) cluster(ccode) idcluster(newcluster) /// 
dots level(90) bca saving("Static CCE IV for M minus X", double replace): bootstaticCCEIV if incflag==1 
// seed chosen as random number 0-10000 from http://www.random.org/

/* Summarise results: */
estat bootstrap, all

/* Calculate test statistic */
preserve
use "Static CCE IV for M minus X.dta", clear
di beta_FEIV
di beta_CCEIV
correlate FEIV CCEIV, covariance
scalar var_FEIV = r(Var_1)
scalar var_CCEIV = r(Var_2)
scalar cov = r(cov_12)
scalar chi2 = ((beta_FEIV - beta_CCEIV)^2)/(var_FEIV + var_CCEIV - 2*cov)
di chi2
di "p-value is " 1 - chi2(1,chi2)
di "p-value is " chi2tail(1,chi2)
restore

/* Check timer and close log file. */
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
log close


capture log close
log using "`ouranalysis'/Bootstrap/Dynamic CCE IV for C; `reps' replications", text replace
******************************************************************************
* BOOTSTRAP FOR DYNAMIC MODEL - HOUSEHOLD CONSUMPTION
******************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set up data and start timer */
timer clear
timer on 1
sort ccode periodid
xtset ccode periodid
replace depvar = hconsGDP
replace lagdepvar = L.depvar

/* Run dynamic CCE IV regression once: */
// construct sample means for all variables:
capture drop mdepvar maid minst
capture drop mlagdepvar
bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1
bysort periodid: egen double maid=mean(aidvar) if incflag == 1
bysort periodid: egen double minst=mean(aidiv) if incflag == 1
bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1
// run dynamic CCE IV regression
xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst if incflag==1, /// 
fe cluster(ccode) small // endog(aidvar) first savefirst
scalar beta_CCEIV = _b[aidvar]
// FE IV:
xtivreg2 depvar lagdepvar (aidvar = aidiv) ydummy* if incflag == 1 & incflagsum >= 6, fe cluster(ccode) small 
scalar beta_FEIV = _b[aidvar]


/* Rewrite the bootstrap program for dynamic model */
capture program drop bootstaticCCEIV
program define bootstaticCCEIV, rclass
	syntax [if] [in]
	marksample touse
	xtset newcluster periodid
	// FE IV regresssion:
	xtivreg2 depvar lagdepvar (aidvar = aidiv) ydummy* if `touse', fe 
	return scalar FEIV = _b[aidvar]
	// construct sample means for all variables:
	capture drop mdepvar maid minst
	capture drop mlagdepvar
	bysort periodid: egen double mdepvar=mean(depvar) if `touse'
	bysort periodid: egen double maid=mean(aidvar) if `touse'
	bysort periodid: egen double minst=mean(aidiv) if `touse'
	bysort periodid: egen double mlagdepvar=mean(lagdepvar) if `touse'
	capture drop aidres ivres
	capture drop depvarres
	capture drop lagdepvarres
	qui: xi: xtreg depvar i.newcluster|mdepvar i.newcluster|mlagdepvar i.newcluster|maid i.newcluster|minst if `touse', fe
	predict double depvarres, e
	qui: xi: xtreg aidvar i.newcluster|mdepvar i.newcluster|mlagdepvar i.newcluster|maid i.newcluster|minst if `touse', fe
	predict double aidres, e
	qui: xi: xtreg aidiv i.newcluster|mdepvar i.newcluster|mlagdepvar i.newcluster|maid i.newcluster|minst if `touse', fe
	predict double ivres, e
	qui: xi: xtreg lagdepvar i.newcluster|mdepvar i.newcluster|mlagdepvar i.newcluster|maid i.newcluster|minst if `touse', fe
	predict double lagdepvarres, e
	// run regression
	xtivreg2 depvarres lagdepvarres (aidres = ivres) if `touse', fe
	return scalar CCEIV = _b[aidres]
	return scalar CCEIVlagdep = _b[lagdepvarres]
	return scalar CCEIVaidLR = _b[aidres]/(1-_b[lagdepvarres])
end

/* Conduct the bootstrap: */
xtset, clear
bootstrap FEIV=r(FEIV) CCEIV=r(CCEIV) CCEIVlagdep=r(CCEIVlagdep) CCEIVaidLR=r(CCEIVaidLR), /// 
reps(`reps') seed(701) cluster(ccode) idcluster(newcluster) dots level(90) bca ///
saving("Dyn CCE IV for C", double replace): bootstaticCCEIV if incflag==1 
// seed chosen as random number 0-10000 from http://www.random.org/

/* Summarise results: */
estat bootstrap, all

/* Calculate test statistic */
preserve
use "Dyn CCE IV for C.dta", clear
di beta_FEIV
di beta_CCEIV
correlate FEIV CCEIV, covariance
scalar var_FEIV = r(Var_1)
scalar var_CCEIV = r(Var_2)
scalar cov = r(cov_12)
scalar chi2 = ((beta_FEIV - beta_CCEIV)^2)/(var_FEIV + var_CCEIV - 2*cov)
di chi2
di "p-value is " 1 - chi2(1,chi2)
di "p-value is " chi2tail(1,chi2)
restore

/* Check timer and close log file. */
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
log close


capture log close
log using "`ouranalysis'/Bootstrap/Dynamic CCE IV for G; `reps' replications", text replace
******************************************************************************
* BOOTSTRAP FOR DYNAMIC MODEL - GOVERNMENT CONSUMPTION
******************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set up data and start timer */
timer clear
timer on 1
sort ccode periodid
xtset ccode periodid
replace depvar = gconsGDP
replace lagdepvar = L.depvar

/* Run dynamic CCE IV regression once: */
// construct sample means for all variables:
capture drop mdepvar maid minst
capture drop mlagdepvar
bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1
bysort periodid: egen double maid=mean(aidvar) if incflag == 1
bysort periodid: egen double minst=mean(aidiv) if incflag == 1
bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1
// run dynamic CCE IV regression
xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst if incflag==1, /// 
fe cluster(ccode) small // endog(aidvar) first savefirst
scalar beta_CCEIV = _b[aidvar]
// FE IV:
xtivreg2 depvar lagdepvar (aidvar = aidiv) ydummy* if incflag == 1 & incflagsum >= 6, fe cluster(ccode) small 
scalar beta_FEIV = _b[aidvar]

/* Conduct the bootstrap: */
xtset, clear
bootstrap FEIV=r(FEIV) CCEIV=r(CCEIV) CCEIVlagdep=r(CCEIVlagdep) CCEIVaidLR=r(CCEIVaidLR), /// 
reps(`reps') seed(4102) cluster(ccode) idcluster(newcluster) dots level(90) bca ///
saving("Dyn CCE IV for G", double replace): bootstaticCCEIV if incflag==1 
// seed chosen as random number 0-10000 from http://www.random.org/

/* Summarise results: */
estat bootstrap, all

/* Calculate test statistic */
preserve
use "Dyn CCE IV for G.dta", clear
di beta_FEIV
di beta_CCEIV
correlate FEIV CCEIV, covariance
scalar var_FEIV = r(Var_1)
scalar var_CCEIV = r(Var_2)
scalar cov = r(cov_12)
scalar chi2 = ((beta_FEIV - beta_CCEIV)^2)/(var_FEIV + var_CCEIV - 2*cov)
di chi2
di "p-value is " 1 - chi2(1,chi2)
di "p-value is " chi2tail(1,chi2)
restore

/* Check timer and close log file. */
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
log close


capture log close
log using "`ouranalysis'/Bootstrap/Dynamic CCE IV for C plus G; `reps' replications", text replace
******************************************************************************
* BOOTSTRAP FOR DYNAMIC MODEL - TOTAL CONSUMPTION
******************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set up data and start timer */
timer clear
timer on 1
sort ccode periodid
xtset ccode periodid
replace depvar = tconsGDPsum
replace lagdepvar = L.depvar

/* Run dynamic CCE IV regression once: */
// construct sample means for all variables:
capture drop mdepvar maid minst
capture drop mlagdepvar
bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1
bysort periodid: egen double maid=mean(aidvar) if incflag == 1
bysort periodid: egen double minst=mean(aidiv) if incflag == 1
bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1
// run dynamic CCE IV regression
xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst if incflag==1, /// 
fe cluster(ccode) small // endog(aidvar) first savefirst
scalar beta_CCEIV = _b[aidvar]
// FE IV:
xtivreg2 depvar lagdepvar (aidvar = aidiv) ydummy* if incflag == 1 & incflagsum >= 6, fe cluster(ccode) small 
scalar beta_FEIV = _b[aidvar]

/* Conduct the bootstrap: */
xtset, clear
bootstrap FEIV=r(FEIV) CCEIV=r(CCEIV) CCEIVlagdep=r(CCEIVlagdep) CCEIVaidLR=r(CCEIVaidLR), /// 
reps(`reps') seed(4945) cluster(ccode) idcluster(newcluster) dots level(90) bca ///
saving("Dyn CCE IV for C plus G", double replace): bootstaticCCEIV if incflag==1 
// seed chosen as random number 0-10000 from http://www.random.org/

/* Summarise results: */
estat bootstrap, all

/* Calculate test statistic */
preserve
use "Dyn CCE IV for C plus G.dta", clear
di beta_FEIV
di beta_CCEIV
correlate FEIV CCEIV, covariance
scalar var_FEIV = r(Var_1)
scalar var_CCEIV = r(Var_2)
scalar cov = r(cov_12)
scalar chi2 = ((beta_FEIV - beta_CCEIV)^2)/(var_FEIV + var_CCEIV - 2*cov)
di chi2
di "p-value is " 1 - chi2(1,chi2)
di "p-value is " chi2tail(1,chi2)
restore

/* Check timer and close log file. */
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
log close


capture log close
log using "`ouranalysis'/Bootstrap/Dynamic CCE IV for I; `reps' replications", text replace
******************************************************************************
* BOOTSTRAP FOR DYNAMIC MODEL - INVESTMENT
******************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set up data and start timer */
timer clear
timer on 1
sort ccode periodid
xtset ccode periodid
replace depvar = grcapformGDP
replace lagdepvar = L.depvar

/* Run dynamic CCE IV regression once: */
// construct sample means for all variables:
capture drop mdepvar maid minst
capture drop mlagdepvar
bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1
bysort periodid: egen double maid=mean(aidvar) if incflag == 1
bysort periodid: egen double minst=mean(aidiv) if incflag == 1
bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1
// run dynamic CCE IV regression
xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst if incflag==1, /// 
fe cluster(ccode) small // endog(aidvar) first savefirst
scalar beta_CCEIV = _b[aidvar]
// FE IV:
xtivreg2 depvar lagdepvar (aidvar = aidiv) ydummy* if incflag == 1 & incflagsum >= 6, fe cluster(ccode) small 
scalar beta_FEIV = _b[aidvar]

/* Conduct the bootstrap: */
xtset, clear
bootstrap FEIV=r(FEIV) CCEIV=r(CCEIV) CCEIVlagdep=r(CCEIVlagdep) CCEIVaidLR=r(CCEIVaidLR), /// 
reps(`reps') seed(3121) cluster(ccode) idcluster(newcluster) dots level(90) bca ///
saving("Dyn CCE IV for I", double replace): bootstaticCCEIV if incflag==1 
// seed chosen as random number 0-10000 from http://www.random.org/

/* Summarise results: */
estat bootstrap, all

/* Calculate test statistic */
preserve
use "Dyn CCE IV for I.dta", clear
di beta_FEIV
di beta_CCEIV
correlate FEIV CCEIV, covariance
scalar var_FEIV = r(Var_1)
scalar var_CCEIV = r(Var_2)
scalar cov = r(cov_12)
scalar chi2 = ((beta_FEIV - beta_CCEIV)^2)/(var_FEIV + var_CCEIV - 2*cov)
di chi2
di "p-value is " 1 - chi2(1,chi2)
di "p-value is " chi2tail(1,chi2)
restore

/* Check timer and close log file. */
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
log close


capture log close
log using "`ouranalysis'/Bootstrap/Dynamic CCE IV for X; `reps' replications", text replace
******************************************************************************
* BOOTSTRAP FOR DYNAMIC MODEL - EXPORT
******************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set up data and start timer */
timer clear
timer on 1
sort ccode periodid
xtset ccode periodid
replace depvar = exportGDP
replace lagdepvar = L.depvar

/* Run dynamic CCE IV regression once: */
// construct sample means for all variables:
capture drop mdepvar maid minst
capture drop mlagdepvar
bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1
bysort periodid: egen double maid=mean(aidvar) if incflag == 1
bysort periodid: egen double minst=mean(aidiv) if incflag == 1
bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1
// run dynamic CCE IV regression
xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst if incflag==1, /// 
fe cluster(ccode) small // endog(aidvar) first savefirst
scalar beta_CCEIV = _b[aidvar]
// FE IV:
xtivreg2 depvar lagdepvar (aidvar = aidiv) ydummy* if incflag == 1 & incflagsum >= 6, fe cluster(ccode) small 
scalar beta_FEIV = _b[aidvar]

/* Conduct the bootstrap: */
xtset, clear
bootstrap FEIV=r(FEIV) CCEIV=r(CCEIV) CCEIVlagdep=r(CCEIVlagdep) CCEIVaidLR=r(CCEIVaidLR), /// 
reps(`reps') seed(7741) cluster(ccode) idcluster(newcluster) dots level(90) bca ///
saving("Dyn CCE IV for X", double replace): bootstaticCCEIV if incflag==1 
// seed chosen as random number 0-10000 from http://www.random.org/

/* Summarise results: */
estat bootstrap, all

/* Calculate test statistic */
preserve
use "Dyn CCE IV for X.dta", clear
di beta_FEIV
di beta_CCEIV
correlate FEIV CCEIV, covariance
scalar var_FEIV = r(Var_1)
scalar var_CCEIV = r(Var_2)
scalar cov = r(cov_12)
scalar chi2 = ((beta_FEIV - beta_CCEIV)^2)/(var_FEIV + var_CCEIV - 2*cov)
di chi2
di "p-value is " 1 - chi2(1,chi2)
di "p-value is " chi2tail(1,chi2)
restore

/* Check timer and close log file. */
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
log close


capture log close
log using "`ouranalysis'/Bootstrap/Dynamic CCE IV for M; `reps' replications", text replace
******************************************************************************
* BOOTSTRAP FOR DYNAMIC MODEL - IMPORT
******************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set up data and start timer */
timer clear
timer on 1
sort ccode periodid
xtset ccode periodid
replace depvar = importGDP
replace lagdepvar = L.depvar

/* Run dynamic CCE IV regression once: */
// construct sample means for all variables:
capture drop mdepvar maid minst
capture drop mlagdepvar
bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1
bysort periodid: egen double maid=mean(aidvar) if incflag == 1
bysort periodid: egen double minst=mean(aidiv) if incflag == 1
bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1
// run dynamic CCE IV regression
xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst if incflag==1, /// 
fe cluster(ccode) small // endog(aidvar) first savefirst
scalar beta_CCEIV = _b[aidvar]
// FE IV:
xtivreg2 depvar lagdepvar (aidvar = aidiv) ydummy* if incflag == 1 & incflagsum >= 6, fe cluster(ccode) small 
scalar beta_FEIV = _b[aidvar]

/* Conduct the bootstrap: */
xtset, clear
bootstrap FEIV=r(FEIV) CCEIV=r(CCEIV) CCEIVlagdep=r(CCEIVlagdep) CCEIVaidLR=r(CCEIVaidLR), /// 
reps(`reps') seed(3675) cluster(ccode) idcluster(newcluster) dots level(90) bca ///
saving("Dyn CCE IV for M", double replace): bootstaticCCEIV if incflag==1 
// seed chosen as random number 0-10000 from http://www.random.org/

/* Summarise results: */
estat bootstrap, all

/* Calculate test statistic */
preserve
use "Dyn CCE IV for M.dta", clear
di beta_FEIV
di beta_CCEIV
correlate FEIV CCEIV, covariance
scalar var_FEIV = r(Var_1)
scalar var_CCEIV = r(Var_2)
scalar cov = r(cov_12)
scalar chi2 = ((beta_FEIV - beta_CCEIV)^2)/(var_FEIV + var_CCEIV - 2*cov)
di chi2
di "p-value is " 1 - chi2(1,chi2)
di "p-value is " chi2tail(1,chi2)
restore

/* Check timer and close log file. */
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
log close


capture log close
log using "`ouranalysis'/Bootstrap/Dynamic CCE IV for M minus X; `reps' replications", text replace
******************************************************************************
* BOOTSTRAP FOR DYNAMIC MODEL - NET IMPORT
******************************************************************************
// repeat variable choices to avoid mistakes:
replace aidvar = netaidGDP
replace aidiv = netaidinstr6070GDP

/* Set up data and start timer */
timer clear
timer on 1
sort ccode periodid
xtset ccode periodid
replace depvar = netimportGDP
replace lagdepvar = L.depvar

/* Run dynamic CCE IV regression once: */
// construct sample means for all variables:
capture drop mdepvar maid minst
capture drop mlagdepvar
bysort periodid: egen double mdepvar=mean(depvar) if incflag == 1
bysort periodid: egen double maid=mean(aidvar) if incflag == 1
bysort periodid: egen double minst=mean(aidiv) if incflag == 1
bysort periodid: egen double mlagdepvar=mean(lagdepvar) if incflag == 1
// run dynamic CCE IV regression
xi: xtivreg2 depvar lagdepvar (aidvar = aidiv) i.ccode|mdepvar i.ccode|maid i.ccode|mlagdepvar i.ccode|minst if incflag==1, /// 
fe cluster(ccode) small // endog(aidvar) first savefirst
scalar beta_CCEIV = _b[aidvar]
// FE IV:
xtivreg2 depvar lagdepvar (aidvar = aidiv) ydummy* if incflag == 1 & incflagsum >= 6, fe cluster(ccode) small 
scalar beta_FEIV = _b[aidvar]

/* Conduct the bootstrap: */
xtset, clear
bootstrap FEIV=r(FEIV) CCEIV=r(CCEIV) CCEIVlagdep=r(CCEIVlagdep) CCEIVaidLR=r(CCEIVaidLR), /// 
reps(`reps') seed(2258) cluster(ccode) idcluster(newcluster) dots level(90) bca ///
saving("Dyn CCE IV for M minus X", double replace): bootstaticCCEIV if incflag==1 
// seed chosen as random number 0-10000 from http://www.random.org/

/* Summarise results: */
estat bootstrap, all

/* Calculate test statistic */
preserve
use "Dyn CCE IV for M minus X.dta", clear
di beta_FEIV
di beta_CCEIV
correlate FEIV CCEIV, covariance
scalar var_FEIV = r(Var_1)
scalar var_CCEIV = r(Var_2)
scalar cov = r(cov_12)
scalar chi2 = ((beta_FEIV - beta_CCEIV)^2)/(var_FEIV + var_CCEIV - 2*cov)
di chi2
di "p-value is " 1 - chi2(1,chi2)
di "p-value is " chi2tail(1,chi2)
restore

/* Check timer and close log file. */
timer off 1
timer list 1
di "Time in minutes: " r(t1)/60
log close
